package com.tuo51.manager.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tuo51.evaluate.domain.appusers.PayWxOrder;
import com.tuo51.evaluate.domain.appusers.Vip;
import com.tuo51.core.domain.org.Organization;
import com.tuo51.evaluate.repositories.appusers.PayWxOrderRepository;
import com.tuo51.evaluate.repositories.appusers.PromoCodeRepository;
import com.tuo51.core.service.StaffService;
import com.tuo51.evaluate.service.appuser.ProductService;
import com.tuo51.evaluate.vo.PayWxOrderVO;
import com.tuo51.evaluate.vo.VipVO;
import com.tuo51.foudation.model.MemVerificationCache;
import com.tuo51.foudation.model.StatusResponse;
import com.tuo51.foudation.utils.CopyUtils;
import com.tuo51.manager.vo.OrganizationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static com.tuo51.foudation.constant.APPConstants.PRODUCT_ID_KEY;
import static com.tuo51.foudation.constant.RequestConstant.TICKET;
import static org.springframework.web.bind.annotation.RequestMethod.GET;

@RestController
@RequestMapping("/mapi/payment")
@Api(value = "/mapi/payment", tags = "OP - 交易记录")
public class MAPIPaymentController {

    @Autowired
    PayWxOrderRepository payWxOrderRepository;

    @Autowired
    ProductService productService;

    @Autowired
    StaffService staffService;

    @Autowired
    PromoCodeRepository promoCodeRepository;

    @Autowired
    PayWxOrderRepository payOrderRepository;

    @Autowired
    MemVerificationCache cache;

    @RequestMapping(value = "/list", method = GET)
    @ApiOperation(value = "微信支付列表")
    public StatusResponse userlist(@RequestHeader(TICKET) String ticket,
                                   @RequestParam(value = "pageNum", required = false, defaultValue = "0")Integer pageNum,
                                   @RequestParam(value = "pageSize", required = false, defaultValue = "15")Integer pageSize,
                                  @RequestParam(value = "promoCodeId", required = false) String promoCodeId){

        cache.getStaffId(ticket);

        PageRequest pageRequest = PageRequest.of(pageNum, pageSize);

        Page<PayWxOrder> result = null;
        if (StringUtils.isEmpty(promoCodeId)) {
            result = payOrderRepository.findAllByOrderByCreateTimeDesc(pageRequest);
        }else{
            result = payOrderRepository.findAllByPromoCodeIdOrderByCreateTimeDesc(promoCodeId, pageRequest);
        }

        Page<PayWxOrderVO> ret = result.map(k->{

            PayWxOrderVO vo = PayWxOrderVO.of(k);

            if (StringUtils.isNotEmpty(k.getAttach())){
                JSONObject attachJson = JSON.parseObject(k.getAttach());
                Long productId = attachJson.getLong(PRODUCT_ID_KEY);

                if (productId != null){
                    Vip vip = productService.getProductById(productId);
                    if (vip != null) {
                        VipVO product = new VipVO();
                        CopyUtils.copyProperties(vip, product);
                        vo.setProduct(product);
                    }
                }
            }
            if (k.getUser() != null){
                Organization org = staffService.staffOrgnization(k.getUser());
                vo.setOrg(OrganizationVO.of(org));
            }

            return vo;
        });

        return StatusResponse.success(ret);
    }
}
